// 无总页数 按照翻页获取数据

var fs = require('fs')
var rp = require('request-promise')
var cheerio = require('cheerio')

var config = require('./config.js')
var util = require('./util/_default.js')
var xlsx = require('node-xlsx')
var xdl = require('./xdl.js')

var helper = util.helper
var dbHelper = util.dbHelper
var logHelper = util.logHelper
var yzmHelper = util.yzmHelper

var dt = [['序号', '产品编号', '药品属性', '通用名', '剂型', '规格',
    '剂型说明', '包装', '包材说明', '附加装置', '生产企业', '投标企业', '挂网价']]

var href = 'http://ypzb2015gs.lnypcg.com.cn/BidInfoPublic/Genuine/GB_ExpertPriceResult5.aspx'

function main(page) {
    var opts = helper.deepCopy(config.reqOpts)
    opts.url = href
    opts.method = 'GET'
    opts.headers.Referer = href+'?WebShieldSessionVerify=Hiq6HFpCbkzwcf3QJf66'

    rp(opts).then(function (body) {

        var $ = cheerio.load(body)
        var data = helper.getFormData($('form#aspnetForm').serializeArray())
        var tr = $('tr', 'table.mainlist')
        //console.log(body)
        if (tr.length > 1) {
            tr.slice(1).each(function () {
                var row = []
                row.push(
                    iGetInnerText($("td", this).eq(0).text()),
                    iGetInnerText($("td", this).eq(1).text()),
                    iGetInnerText($("td", this).eq(2).text()),
                    iGetInnerText($("td", this).eq(3).text()),
                    iGetInnerText($("td", this).eq(4).text()),
                    iGetInnerText($("td", this).eq(5).text()),
                    iGetInnerText($("td", this).eq(6).text()),
                    iGetInnerText($("td", this).eq(7).text()),
                    iGetInnerText($("td", this).eq(8).text()),
                    iGetInnerText($("td", this).eq(9).text()),
                    iGetInnerText($("td", this).eq(10).text()),
                    iGetInnerText($("td", this).eq(11).text()),
                    iGetInnerText($("td", this).eq(12).text())
                )
                dt.push(row)
            });
            GetDetail(data, page)
        }
        else {
            console.log("未获取到数据！")
            setTimeout(function () {
                main(page)
            }, Math.random() * 10 * 1000)

        }
        //data['ctl00$cphMaster$btnQuery'] = '查询'
        //GetDetail(data, page)

    }).catch(function (err) {
        console.log(err)
        setTimeout(function () {
            main()
        }, Math.random() * 10 * 1000)
    })
}
function GetDetail(data, page) {
    console.log('当前页：' + page + ',当前总数：' + dt.length)
    var opts = helper.deepCopy(config.reqOpts)
    data['__EVENTTARGET'] = 'ctl00$cphMaster$HzPager1'
    data['__EVENTARGUMENT'] = page
    opts.url = href
    opts.method = 'POST'
    opts.headers.Referer = href
    opts.formData = data
    rp(opts).then(function (body) {
        if(page==20){
            console.log(body)
        }
        var $ = cheerio.load(body)
        var ainfo = $('a', 'div#ctl00_cphMaster_HzPager1')
        var tr = $('tr', 'table.mainlist')
        if (tr.length > 1) {
            tr.slice(1).each(function () {
                var row = []
                row.push(
                    iGetInnerText($("td", this).eq(0).text()),
                    iGetInnerText($("td", this).eq(1).text()),
                    iGetInnerText($("td", this).eq(2).text()),
                    iGetInnerText($("td", this).eq(3).text()),
                    iGetInnerText($("td", this).eq(4).text()),
                    iGetInnerText($("td", this).eq(5).text()),
                    iGetInnerText($("td", this).eq(6).text()),
                    iGetInnerText($("td", this).eq(7).text()),
                    iGetInnerText($("td", this).eq(8).text()),
                    iGetInnerText($("td", this).eq(9).text()),
                    iGetInnerText($("td", this).eq(10).text()),
                    iGetInnerText($("td", this).eq(11).text()),
                    iGetInnerText($("td", this).eq(12).text())
                )
                dt.push(row)
            });

            if (dt.length % 200 == 0) {
                saveExcel(dt, page)
            }
            //console.log(ainfo.eq(1).attr('disabled'))
            if (ainfo.eq(1).attr('disabled') != 'disabled') {
                page = page + 1
                GetDetail(data, page)
            } else {
                saveExcel(dt, page)
            }
        }
        else {
            console.log("未获取到数据！")
            setTimeout(function () {
                GetDetail(data, page)
            }, Math.random() * 10 * 1000)

        }
    }).catch(function (err) {
        console.log('抓取异常：' + err)
        setTimeout(function () {
            GetDetail(data, page)
        }, Math.random() * 10 * 1000)
    })

}

function iGetInnerText(testStr) {
    var resultStr = testStr.replace(/\ +/g, ""); //去掉空格
    resultStr = testStr.replace(/[ ]/g, "");    //去掉空格
    resultStr = testStr.replace(/[\t\r\n]/g, ""); //去掉回车换行
    resultStr = testStr.replace(/[ ]/g, "");    //去掉空格
    return resultStr;
}

function saveExcel(data, page) {
    d = (new Date()).Format('hhmmss')
    var file = xlsx.build([{ name: "mySheetName", data: data }])
    fs.writeFileSync('result_' + page + '_' + d + '.xlsx', file, 'binary')
}


main(2)



